// pages/mine/index.js
Page({
  data: {
    userInfo: null,
    todoStats: {
      total: 0,
      ongoing: 0,
      completed: 0
    },
    userRole: 1, // 1-普通用户，2-管理员
    functionList: [
      { id: 'myTodos', name: '我负责的', icon: 'icon-todo' },
      { id: 'mySupervisor', name: '我监督的', icon: 'icon-supervisor' },
      { id: 'myCollaborator', name: '我协同的', icon: 'icon-collaborator' },
      { id: 'settings', name: '设置', icon: 'icon-settings' },
      { id: 'userManagement', name: '用户管理', icon: 'icon-users', adminOnly: true }
    ]
  },

  onLoad: function() {
    this.getUserInfo();
  },
  
  onShow: function() {
    this.loadTodoStats();
  },

  // 获取用户信息
  getUserInfo: function() {
    const app = getApp();
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        userRole: app.globalData.userRole
      });
    } else {
      // 如果还没有获取到用户信息，可以提示用户授权
      wx.showModal({
        title: '提示',
        content: '请先授权获取用户信息',
        showCancel: false
      });
    }
  },

  // 加载待办统计
  loadTodoStats: function() {
    const app = getApp();
    const todos = app.globalData.todos;
    const userInfo = app.globalData.userInfo;
    
    if (!userInfo) return;
    
    // 获取与当前用户相关的待办
    const myTodos = todos.filter(todo => 
      todo.responsible === userInfo.nickName || 
      todo.supervisor === userInfo.nickName || 
      (todo.collaborators && todo.collaborators.includes(userInfo.nickName))
    );
    
    this.setData({
      todoStats: {
        total: myTodos.length,
        ongoing: myTodos.filter(todo => todo.status === 'ongoing').length,
        completed: myTodos.filter(todo => todo.status === 'completed').length
      }
    });
  },

  // 跳转到功能页面
  navigateToFunction: function(e) {
    const funcId = e.currentTarget.dataset.id;
    
    switch(funcId) {
      case 'myTodos':
        wx.navigateTo({
          url: '/pages/index/index?filter=responsible'
        });
        break;
      case 'mySupervisor':
        wx.navigateTo({
          url: '/pages/index/index?filter=supervisor'
        });
        break;
      case 'myCollaborator':
        wx.navigateTo({
          url: '/pages/index/index?filter=collaborator'
        });
        break;
      case 'settings':
        wx.showToast({
          title: '设置功能开发中',
          icon: 'none'
        });
        break;
      case 'userManagement':
        // 检查是否有管理员权限
        if (this.data.userRole === 2) {
          wx.navigateTo({
            url: '/pages/admin/user-management'
          });
        } else {
          wx.showToast({
            title: '权限不足',
            icon: 'none'
          });
        }
        break;
    }
  },

  // 退出登录
  logout: function() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          const app = getApp();
          app.globalData.userInfo = null;
          this.setData({
            userInfo: null
          });
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          });
        }
      }
    });
  }
}));